<template>
  <div class="login">
    <div class="container">
      <div class="avatar">
        <img src="../assets/images/avatar.jpg" alt="" />
      </div>
      <el-form
        :model="loginForm"
        ref="ruleForm"
        class="demo-ruleForm"
        :rules="loginRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-s-custom"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-key"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="login-btn" @click="loginFn"
          >登陆</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user.js"; // @代表src文件夹
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          // 一定要输入
          { required: true, message: "请输入手机号", trigger: "blur" },
          // 如果输入，还要输入正确
          // pattern:指定正则规则
          {
            pattern: /^.{5,11}$/,
            message: "请输入5-11位的账号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            pattern: /^.{3,16}$/,
            message: "请输入3-16位的密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 登录
    loginFn() {
      // 判断用户输入不能为空
      if (!this.loginForm.username) {
        this.$message.error("用户不能为空");
        return;
      }
      if (!this.loginForm.password) {
        this.$message.error("密码不能为空");
        return;
      }
      // 加载效果 (全屏)：作用: 1 提高用户体验  2 避免用户反复点击，造成重复请求问题(另一种方法“防抖节流”)
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      login(this.loginForm).then((res) => {
        // 关闭加载效果
        loading.close();
        if (res.data.message == "登录成功") {
          // 保存token和用户id
          localStorage.setItem("token_backend", res.data.data.token);
          localStorage.setItem("id_backend", res.data.data.user.id);
          // 登录成功跳转首页
          this.$router.push("/layout");
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>